﻿#region 信息
// Copyright©2014 Rex Rao all rights reserved.
// File name : Mojito\Mojito.Service\DishService.cs
// Author : Sohobloo
// Create time : 2014/08/29 15:40
// Update time : 2014/08/29 15:40
#endregion

using System;
using System.ComponentModel.Composition;
using System.Linq;
using Mojito.Data.Repository;
using Mojito.Entity;
using Sohobloo.SoCommon;
using Sohobloo.SoCommon.CommonExtension;
using Sohobloo.SoMcCoffee.Entity;
using Sohobloo.SoMcCoffee.Service;

namespace Mojito.Service.Impl
{
    [Export(typeof(IDishService))]
    internal class DishService : ServiceBase, IDishService
    {
        [Import]
        private IDishRepository DishRepository { get; set; }

        [Import]
        private IDishCategoryRepository DishCategoryRepository { get; set; }

        [Import]
        private IOrderDishRepository OrderDishRepository { get; set; }

        public Dish GetDish(Guid dishId)
        {
            return DishRepository.GetByKey(dishId);
        }

        public OperationResult AddDish(Guid? categoryId, string dishName, double price, DishTagType dishTag)
        {
            ArgumentHelper.Instance
                          .CheckEmpty(dishName)
                          .CheckRange(price, 0, 9999);
            var existed = DishRepository.Entities.Any(d => d.DishName == dishName
                                                           && d.DataStatus != DataStatus.Deleted);
            if(existed)
            {
                return new OperationResult(OperationResultType.Exist, "Dish name exist.");
            }

            var dish = new Dish
            {
                CategoryId = categoryId,
                DishName = dishName,
                Price = price,
                DishTag = dishTag
            };

            DishRepository.Insert(dish);
            var result = Commit();
            result.AppendData = dish;
            return result;
        }

        public OperationResult UpdateDish(Guid dishId, Guid? categoryId, string dishName, double price, DishTagType dishTag)
        {
            ArgumentHelper.Instance
              .CheckEmpty(dishName)
              .CheckRange(price, 0, 9999);
            var existed = DishRepository.Entities.Any(d => d.DishName == dishName
                                                           && d.DataStatus != DataStatus.Deleted
                                                           && d.Id != dishId
                );
            if (existed)
            {
                return new OperationResult(OperationResultType.Exist, "Dish name exist.");
            }
            var dish = DishRepository.GetByKey(dishId);
            if (dish.IsNull())
            {
                return new OperationResult(OperationResultType.NotExist, "Dish not exist.");
            }

            dish.CategoryId = categoryId;
            dish.DishName = dishName;
            dish.Price = price;
            dish.DishTag = dishTag;

            DishRepository.Update(dish);
            var result = Commit();
            result.AppendData = dish;
            return result;
        }

        public OperationResult DeleteDish(Guid dishId)
        {
            // 检查引用（OrderDish）
            var referenced = OrderDishRepository.Entities.Any(od => od.DishId == dishId);
            if (referenced)
            {
                var dish = DishRepository.GetByKey(dishId);
                if (dish.IsNull())
                {
                    return new OperationResult(OperationResultType.NotExist, "Dish not exist.");
                }
                dish.DataStatus = DataStatus.Deleted;
                DishRepository.Update(dish);
            }
            else
            {
                DishRepository.Delete(dishId);
            }

            return Commit();
        }

        public OperationResult SortDish(Guid dishId, Guid? prevDishId)
        {
            DishRepository.ChangeSort(dishId, prevDishId);
            return Commit();
        }
    }
}